import type { Order } from "../types/reservation";
import { request } from "@/service/request/request";

/**
 * 提交订单
 * @param order
 * @returns
 */
export const submitOrder = async (order: Order) => {
    try {
        return await request<string>("/order/submit_order", "post", order);
    } catch (error) {
        throw error;
    }
};

/**
 * 查找客户是否有未支付的订单
 * @param customerId
 * @returns
 */
export const getUnpaidOrder = async (customerId: string) => {
    try {
        return await request<Order>(
            `/order/get_unpaid_order/${customerId}`,
            "post",
            null
        );
    } catch (error) {
        throw error;
    }
};

/**
 * 查找客户是否有过期的订单 -> 未支付或者主动取消都视为过期
 * @param customerId
 * @returns
 */
export const getExpiredOrders = async (customerId: string) => {
    try {
        return await request<Order[]>(
            `/order/customer_getOrder_expired/${customerId}`,
            "post",
            null
        );
    } catch (error) {
        throw error;
    }
};

export const deleteExpiredOrders = async (customerId: string) => {
    try {
        return await request<null>(
            `/order/customer_deleteOrder_expired/${customerId}`,
            "post",
            null
        );
    } catch (error) {
        throw error;
    }
};

/**
 * 模拟支付订单接口
 * @param reservation
 * @returns
 */
export const payOrder = async (orderId: string) => {
    try {
        return await request<null>(`/order/pay_order/${orderId}`, "post", null);
    } catch (error) {
        throw error;
    }
};

/**
 * 客户取消订单，但是订单不会删除，而是会被标记为已取消
 * @param orderId
 * @returns
 */
export const cancelOrder = async (orderId: string) => {
    try {
        return await request<null>(
            `/order/cancel_order/${orderId}`,
            "post",
            null
        );
    } catch (error) {
        throw error;
    }
};

/**
 * 根据订单id获取订单信息
 * @param orderId
 * @returns
 */
export const getOrderById = async (orderId: string) => {
    try {
        return await request<Order>(
            `/order/get_order_by_id/${orderId}`,
            "post",
            null,
            { loading: false }
        );
    } catch (error) {
        throw error;
    }
};
